﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace Cwing.UI.Controls
{
    public class ErrorList : Panel
    {
        private String title;

        [Browsable(true), Category("Validation")]
        public String Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        [Category("Validation")]
        public override bool Visible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
            }
        }

        private String titleCssClass;

        [Category("Validation")]
        public String TitleCssClass
        {
            get
            {
                return titleCssClass;
            }
            set
            {
                titleCssClass = value;
            }
        }

        private String messageCssClass;

        [Category("Validation")]
        public String MessageCssClass
        {
            get
            {
                return messageCssClass;
            }
            set
            {
                messageCssClass = value;
            }
        }

        [Category("Validation")]
        public Boolean UseForMandatory { get; set; }

        /// <summary>
        /// The ViewState is disabled
        /// </summary>
        public override bool EnableViewState
        {
            get
            {
                return false;
            }
            set
            {
            }
        }

        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                base.RenderBeginTag(writer);
            }
            else
            {
                writer.Write(String.Format("<ul id=\"{0}\" {1}>", this.ClientID, String.IsNullOrEmpty(CssClass) ? "" : String.Format("class=\"{0}\"", CssClass)));
            }
        }

        public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                base.RenderEndTag(writer);
            }
            else
            {
                writer.Write("</ul>");
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                if (!String.IsNullOrEmpty(Title))
                {
                    writer.Write(Title);
                }
            }
            else
            {
                base.Render(writer);
            }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            if (ErrorMessages.Count > 0)
            {
                if (!String.IsNullOrEmpty(Title))
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl("li");
                    li.InnerText = Title;
                    if (!String.IsNullOrEmpty(TitleCssClass))
                    {
                        li.Attributes["class"] = TitleCssClass;
                    }
                    Controls.Add(li);
                }

                for (int i = 0; i < ErrorMessages.Count; i++)
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl("li");
                    li.InnerText = ErrorMessages[i];
                    if (!String.IsNullOrEmpty(MessageCssClass))
                    {
                        li.Attributes["class"] = MessageCssClass;
                    }
                    Controls.Add(li);
                }
            }
            ChildControlsCreated = true;
        }

        private List<string> errormessages = new List<string>();

        [Browsable(false)]
        public List<string> ErrorMessages
        {
            get
            {
                return errormessages;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!DesignMode)
            {
                bool _UseForClient = false;

                _UseForClient = UseForClient;

                if (_UseForClient == false && ErrorMessages.Count <= 0)
                {
                    Visible = false;
                }

                if (Visible)
                {
                    //rend client script
                    if (_UseForClient == true)
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(ErrorList), this.ClientID, String.Format("CW.AddErrorList({0}ID:\"{1}\",Title:\"{2}\",TC:\"{3}\",MC:\"{4}\",M:{5}{6});\r\n", "{", ClientID, Title, TitleCssClass, MessageCssClass, UseForMandatory.ToString().ToLower(), "}"), true);
                    }
                }

            }
        }

        [Browsable(false)]
        public Boolean UseForClient { get; internal set; }

    }
}
